package site.xuqing.androidbase.core;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Iterator;

import androidx.annotation.CallSuper;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import site.xuqing.androidbase.util.LogUtil;

/**
 * @author xuqing
 * @Description 基础fragment
 * @createTime 2021年12月10日 11:18:49
 */
public abstract class BaseFragment extends Fragment implements IBaseFragment, IBaseContext {
    private View rootView;
    
    @Nullable
    @Override
    @CallSuper
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayout(), container, false);
        }
        initView(rootView);
        initData();
        initEvent();
        return rootView;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        LogUtil.logFragment(getClass().getSimpleName());
    }
    
    @Override
    public <T extends View> T findViewById(@IdRes int id) {
        if (rootView == null) {
            return null;
        }
        return rootView.findViewById(id);
    }
    
    @Override
    public void closeActivity() {
        if (getActivity() != null) {
            getActivity().finish();
        }
    }
    
    @Override
    public void closeAllActivities() {
        if (getActivity() != null) {
            Iterator<BaseActivity> iterator = ((BaseActivity) getActivity()).getBaseActivities().iterator();
            while (iterator.hasNext()) {
                BaseActivity activity = iterator.next();
                if (activity != null) {
                    activity.finish();
                    iterator.remove();
                }
            }
        }
    }
    
    @Override
    public void startActivity(Class<? extends BaseActivity> activity) {
        if (getActivity() != null) {
            startActivity(new Intent(getActivity(), activity));
        }
    }
    
    @Override
    public void startActivity(Class<? extends BaseActivity> activity, Bundle bundle) {
        if (getActivity() != null) {
            startActivity(new Intent(getActivity(), activity).putExtras(bundle));
        }
    }
    
    @Override
    public void startActWithIntent(Intent intent) {
        startActivity(intent);
    }
    
    @Override
    public void startActivityForResult(Class<? extends BaseActivity> activity, int requestCode) {
        if (getActivity() != null) {
            startActivityForResult(new Intent(getActivity(), activity), requestCode);
        }
    }
    
    @Override
    public void startActivityForResult(Class<? extends BaseActivity> activity, Bundle bundle, int requestCode) {
        if (getActivity() != null) {
            startActivityForResult(new Intent(getActivity(), activity).putExtras(bundle), requestCode);
        }
    }
    
    @Override
    public void startActWithIntentForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }
}
